import { defineStore } from 'pinia'

// 资源状态管理
export const useResourceStore = defineStore('resource', {
  state: () => ({
    // 当前教学设计数据
    designData: {
      courseName: '',
      objective: '',
      duration: 45,
      teachingStyle: '',
      // 教学环节
      teachingSteps: [],
      // 教学方法
      teachingMethods: [],
      // 教学重难点
      keyPoints: [],
      difficultyPoints: [],
      // 生成时间
      createdAt: ''
    },
    
    // 当前多媒体资源数据
    mediaData: {
      // 图像资源
      images: [],
      // 视频资源
      videos: [],
      // 知识点描述
      knowledgePoint: '',
      // 风格
      style: '',
      // 生成时间
      createdAt: ''
    },
    
    // 当前练习题数据
    exerciseData: {
      // 知识点
      knowledgePoints: [],
      // 题型配置
      questionTypes: [],
      // 难度分布
      difficultyDistribution: {
        easy: 30,
        medium: 50,
        hard: 20
      },
      // 题目列表
      questions: [],
      // 生成时间
      createdAt: ''
    },
    
    // 当前试卷数据
    paperData: {
      // 考核范围
      examScope: [],
      // 难度分布
      difficultyDistribution: {
        easy: 30,
        medium: 50,
        hard: 20
      },
      // 题型比例
      questionTypeRatio: {},
      // 试卷内容
      content: {
        title: '',
        sections: []
      },
      // 答案
      answers: [],
      // 知识点覆盖率
      coverageRate: 0,
      // 生成时间
      createdAt: ''
    },
    
    // 个性化定制数据
    customData: {
      // 课程大纲
      courseOutline: {},
      // 学情数据
      learningData: [],
      // 推荐资源
      recommendations: [],
      // 生成时间
      createdAt: ''
    }
  }),
  
  getters: {
    // 获取教学设计数据
    getDesignData: (state) => state.designData,
    // 获取教学重点
    getKeyPoints: (state) => state.designData.keyPoints,
    // 获取多媒体资源数据
    getMediaData: (state) => state.mediaData,
    // 获取练习题数据
    getExerciseData: (state) => state.exerciseData,
    // 获取试卷数据
    getPaperData: (state) => state.paperData,
    // 获取个性化定制数据
    getCustomData: (state) => state.customData
  },
  
  actions: {
    // 设置教学设计数据
    setDesignData(data) {
      this.designData = { ...this.designData, ...data }
      this.designData.createdAt = new Date().toLocaleString()
      
      // 自动同步教学重点到其他模块
      if (data.keyPoints && data.keyPoints.length > 0) {
        // 同步到多媒体资源的知识点描述
        this.mediaData.knowledgePoint = data.keyPoints.join('、')
        
        // 同步到练习题的知识点
        this.exerciseData.knowledgePoints = data.keyPoints.map(point => ({
          id: Date.now() + Math.random().toString(36).substr(2, 5),
          name: point
        }))
        
        // 同步到试卷的考核范围
        this.paperData.examScope = data.keyPoints
      }
    },
    
    // 设置多媒体资源数据
    setMediaData(data) {
      this.mediaData = { ...this.mediaData, ...data }
      this.mediaData.createdAt = new Date().toLocaleString()
    },
    
    // 添加图像资源
    addImageResource(image) {
      this.mediaData.images.push({
        id: Date.now() + Math.random().toString(36).substr(2, 5),
        url: image.url,
        description: image.description,
        style: image.style,
        createdAt: new Date().toLocaleString()
      })
    },
    
    // 添加视频资源
    addVideoResource(video) {
      this.mediaData.videos.push({
        id: Date.now() + Math.random().toString(36).substr(2, 5),
        url: video.url,
        description: video.description,
        duration: video.duration,
        style: video.style,
        createdAt: new Date().toLocaleString()
      })
    },
    
    // 设置练习题数据
    setExerciseData(data) {
      this.exerciseData = { ...this.exerciseData, ...data }
      this.exerciseData.createdAt = new Date().toLocaleString()
    },
    
    // 设置试卷数据
    setPaperData(data) {
      this.paperData = { ...this.paperData, ...data }
      this.paperData.createdAt = new Date().toLocaleString()
    },
    
    // 设置个性化定制数据
    setCustomData(data) {
      this.customData = { ...this.customData, ...data }
      this.customData.createdAt = new Date().toLocaleString()
    },
    
    // 清空所有资源数据
    clearAllData() {
      // 重置教学设计数据
      this.designData = {
        courseName: '',
        objective: '',
        duration: 45,
        teachingStyle: '',
        teachingSteps: [],
        teachingMethods: [],
        keyPoints: [],
        difficultyPoints: [],
        createdAt: ''
      }
      
      // 重置多媒体资源数据
      this.mediaData = {
        images: [],
        videos: [],
        knowledgePoint: '',
        style: '',
        createdAt: ''
      }
      
      // 重置练习题数据
      this.exerciseData = {
        knowledgePoints: [],
        questionTypes: [],
        difficultyDistribution: {
          easy: 30,
          medium: 50,
          hard: 20
        },
        questions: [],
        createdAt: ''
      }
      
      // 重置试卷数据
      this.paperData = {
        examScope: [],
        difficultyDistribution: {
          easy: 30,
          medium: 50,
          hard: 20
        },
        questionTypeRatio: {},
        content: {
          title: '',
          sections: []
        },
        answers: [],
        coverageRate: 0,
        createdAt: ''
      }
      
      // 重置个性化定制数据
      this.customData = {
        courseOutline: {},
        learningData: [],
        recommendations: [],
        createdAt: ''
      }
    }
  }
})